<?php

namespace App\Http\Controllers\Api;

use App\Models\Message;
use App\Transformers\MessageTransformer;
use Illuminate\Http\Request;

class MessagesController extends Controller
{
    public function index(Request $request)
    {
        $where = $request->only('type');

        $query = Message::where('user_id', $this->user->id)->where($where);

        $messages = $query;

        $update['is_read'] = 1;

        $messages->update($update);

        $data = $query->paginate();

        return $this->response->paginator($data, new MessageTransformer());
    }

    public function amount(Request $request)
    {
        $where = $request->only('type');

        $amount = Message::where('user_id', $this->user->id)->where($where)->count();

        return response()->json(['message' => 'success', 'code' => 1, 'status' => true, 'data' => ['amount' => $amount]], 200);
    }
}
